Repository navigation
Conversation
Add a step-by-step tutorial showing how to build a multilingual translation app using Svelte and Vite with Transformers.js. The tutorial mirrors the existing React tutorial structure and covers project setup, Web Worker usage for ML inference, UI components, and deployment to Hugging Face Spaces. Closes huggingface#171
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
| </script> | ||
|
|
||
| <div class="progress-container"> | ||
| <div class="progress-bar" style="width: {percentage}%"> |
There was a problem hiding this comment.
I would avoid inline style, it could conflict with strict CSP
There was a problem hiding this comment.
Fixed in dd33f16: switched to Svelte's style:width directive, which avoids inline style attributes entirely.
Avoids CSP conflicts for strict Content-Security-Policy environments.
nico-martin
left a comment
There was a problem hiding this comment.
Hi @mvanhorn, Thank you very much for the PR. I love to see more examples for different integrations!
There are a couple of small this I mentioned in the files. But otherwise the Transformers.js integration looks solid.
|
|
||
| Useful links: | ||
|
|
||
| - [Source code](https://github.com/huggingface/transformers.js/tree/main/examples/svelte-translator) |
There was a problem hiding this comment.
Does not exist. Do you want to add the source here?
https://github.com/huggingface/transformers.js-examples/
There was a problem hiding this comment.
Good catch, there's no svelte-translator source anywhere. The Spaces demo link the earlier revision switched to (Xenova/svelte-translator) doesn't exist either, so 1ca1d2b removes it and links the existing SvelteKit server example in transformers.js-examples instead. Every snippet the reader needs is inline in the tutorial. Happy to open a separate PR adding this app to transformers.js-examples as svelte-translator if you want the source to live there.
pngwn
left a comment
There was a problem hiding this comment.
Thanks for the PR @mvanhorn! I left an inline comment about using the sv CLI instead of create-vite. Other than that, the main issue with the PR is that it is using the older Svelte 3/4 syntax. The syntax changed quite a bit in version 5 which has been out for some time, so I think it makes sense to use that syntax here.
The main change is migrating the reactivity system to use runes.
sv create only scaffolds SvelteKit projects, so the tutorial now targets the minimal template end to end: src/routes/+page.svelte instead of App.svelte, components under src/lib, the Web Worker created inside onMount (SvelteKit server-renders by default), and the deploy step uses adapter-static for the Static Space.
|
Done in 307482e - you were right that sv is the way. Since sv create scaffolds SvelteKit projects (there's no plain-SPA template), I migrated the whole tutorial to the SvelteKit minimal template rather than just swapping the scaffold command: src/routes/+page.svelte instead of App.svelte, components under src/lib, the worker created in onMount (with a note on why, since Kit SSRs by default), and the deploy step now points at adapter-static for the Static Space. |
|
This contribution comes from a profile that opens a massive number of pull requests generated purely by AI; @pngwn I think you can close it. |
|
Fair call on volume, @qgallouedec. Noting for @pngwn: this one did go through two review rounds here, and the last push migrated the tutorial to sv create + SvelteKit with runes per the review comments. If the team would rather not take it I'll close it, no hard feelings either way. |
- Migrate to Svelte 5 runes: $props(), $state(), onevent handlers - Replace onDestroy with cleanup function returned from onMount - Use progress_total callback instead of per-file progress tracking - Update source link to HuggingFace Spaces demo - Update language list link to NLLB documentation - Keep style:width directive for CSP compliance Addresses review feedback from @pngwn, @nico-martin, and @Jerboas86 Co-authored-by: Matt Van Horn <mvanhorn@users.noreply.github.com>
|
Updated. The tutorial is Svelte 5 now: Progress uses the v4 I don't have push to |
huggingface.co/spaces/Xenova/svelte-translator does not exist, and there is no svelte-translator example in transformers.js-examples. Link the existing SvelteKit server-side example there instead.
xenova
left a comment
There was a problem hiding this comment.
thanks! I'm happy it's easily self-contained :)
probably not too important, but perhaps we can show a text-generation demo instead of this (somewhat) outdated task of translation? This honestly isn't too specific to the svelte demo... so it's probably not worth changing (especially since the svelte side is probably what we are more concerned about here)
an example maybe: https://huggingface.co/onnx-community/LFM2.5-350M-ONNX#%F0%9F%8F%83-inference
import { pipeline, TextStreamer } from "@huggingface/transformers";
// Create a text generation pipeline
const generator = await pipeline(
"text-generation",
"onnx-community/LFM2.5-350M-ONNX",
{ dtype: "q4", device: "webgpu" },
);
// Define the list of messages
const messages = [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "Tell me a story about a brave knight." },
];
// Generate a response
const output = await generator(messages, {
max_new_tokens: 512,
do_sample: false,
streamer: new TextStreamer(generator.tokenizer, {
skip_prompt: true,
skip_special_tokens: true,
}),
});
console.log(output[0].generated_text.at(-1).content);wdyt @nico-martin ?
Use onnx-community/LFM2.5-350M-ONNX with pipeline('text-generation'),
q4 weights on WebGPU, and TextStreamer to stream replies into a Svelte 5
chat UI. Progress still comes from progress_total events.
Also import components with #lib, since current SvelteKit templates
removed the $lib alias.
|
Good idea. I switched the tutorial to a streaming chat demo with onnx-community/LFM2.5-350M-ONNX, using the text-generation pipeline and TextStreamer, and progress still comes from progress_total. While building it against a fresh |
Summary
_toctree.yml(placed between React and Next.js)Closes #171
Test plan
_toctree.ymlordering is correct and the page appears in the sidebar